package com.tarim.streetdance.api;

import com.alibaba.fastjson.JSONObject;
import com.tarim.streetdance.model.Article;
import com.tarim.streetdance.model.User;
import com.tarim.streetdance.service.ArticleService;
import com.tarim.streetdance.util.Constants;
import com.tarim.streetdance.util.JsonResult;
import com.tarim.streetdance.util.RedisUtil;
import com.tarim.streetdance.vo.Criteria;
import com.tarim.streetdance.vo.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
@CrossOrigin
@RequestMapping("/api/adminArticle")
public class AdminArticleApi {
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private ArticleService articleService;

    /**
     * 条件查询
     * @param request
     * @param response
     * @param pageIndex
     * @param pageSize
     * @param titleOrContent
     * @return
     */
    @RequestMapping(value = "/selectByCondition")
    public JsonResult selectByCondition(HttpServletRequest request, HttpServletResponse response,
                                        @RequestParam(value = "pageIndex", defaultValue = "0")Integer pageIndex,
                                        @RequestParam(value = "pageSize", defaultValue = "20")Integer pageSize,
                                        @RequestParam(value = "titleOrContent", defaultValue = "")String titleOrContent){
        JsonResult js;
        try {
            PageBean<Article> pageBean = new PageBean<>(pageIndex, pageSize);
            Criteria criteria = new Criteria();
            criteria.addExtField("titleOrContent", titleOrContent);
            pageBean = articleService.selectByCondition(criteria, pageBean);
            js = new JsonResult(Constants.STATUS_SUCCESS, "查询成功", pageBean);
        }catch (Exception ex) {
            ex.printStackTrace();
            js = new JsonResult(Constants.STATUS_ERROR, "系统异常");
        }
        return js;
    }

    /**
     * 修改公告
     * @param request
     * @param article
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public JsonResult update(HttpServletRequest request, Article article){
        JsonResult js;
        try {
            Object object = redisUtil.get(request.getHeader("token"));
            if (object == null) {
                js = new JsonResult(Constants.STATUS_FAIL, "登录后进行操作");
                return js;
            }
            JSONObject jsonObject = (JSONObject) object;
            User user = jsonObject.toJavaObject(User.class);
            if (user.getUserType() != 0) {
                js = new JsonResult(Constants.STATUS_FAIL, "非管理员用户没有权限访问");
                return js;
            }
            int i = articleService.updateByPrimaryKeySelective(article);
            js = new JsonResult(Constants.STATUS_SUCCESS, "操作成功");
        }catch (Exception ex) {
            ex.printStackTrace();
            js = new JsonResult(Constants.STATUS_ERROR, "系统异常");
        }
        return js;
    }

    /**
     * 发布公告
     * @param request
     * @param article
     * @return
     */
    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public JsonResult insert(HttpServletRequest request, Article article){
        JsonResult js;
        try {
            Object object = redisUtil.get(request.getHeader("token"));
            if (object == null) {
                js = new JsonResult(Constants.STATUS_FAIL, "登录后进行操作");
                return js;
            }
            JSONObject jsonObject = (JSONObject) object;
            User user = jsonObject.toJavaObject(User.class);
            if (user.getUserType() != 0) {
                js = new JsonResult(Constants.STATUS_FAIL, "非管理员用户没有权限访问");
                return js;
            }
            int i = articleService.insertSelective(article);
            js = new JsonResult(Constants.STATUS_SUCCESS, "操作成功");
        }catch (Exception ex) {
            ex.printStackTrace();
            js = new JsonResult(Constants.STATUS_ERROR, "系统异常");
        }
        return js;
    }
}
